export type LoginDto = {
    login: string
    password: string
}

export function MakeUserData(login: string, password: string) {
    return { login, password }
}

type DictionaryType = {
    code: string
}
type AutoType = {
    autoCategory: DictionaryType
    model: DictionaryType
}
type PersonType = {
    firstName: string
    lastName: string
    driverLicense: string
    email: string
}

export type ProposalDto = {
    id: string
    status: DictionaryType
    city: DictionaryType
    auto: AutoType
    person: PersonType
}

function MakeDictionary(code: string) {
    return { code }
}
export function MakeAutoData(autoCategory: string, model: string) {
    return { autoCategory: MakeDictionary(autoCategory), model: MakeDictionary(model) }
}
export function MakePersonData(firstName: string, lastName: string, driverLicense: string, email: string) {
    return { firstName, lastName, driverLicense, email }
}

export function MakeProposalData(id: string, statusInfo: string, cityInfo: string, auto: AutoType, person: PersonType) {
    const status = MakeDictionary(statusInfo)
    const city = MakeDictionary(cityInfo)
    return { id, status, city, auto, person }
}

export function Validated(data: ProposalDto) {
    let error = ''
    if (data.auto.autoCategory.code == undefined) error += 'марка автомобиля\n'
    if (data.auto.model.code == undefined) error += 'модель автомобиля\n'
    if (data.city.code == undefined) error += 'город\n'
    if (!data.person.firstName.match("^[А-ЯЁа-яё '-]+$")) error += 'имя\n'
    if (!data.person.lastName.match("^[А-ЯЁа-яё '-]+$")) error += 'фамилия\n'
    if (!data.person.driverLicense.match(/^\d{4} \d{6}$/)) error += 'водительская лицензия\n'
    if (!data.person.email.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)) error += 'адрес электронной почты'
    return error
}
